package com.goods.category.service;

import com.goods.entity.Category;
import com.goods.mapper.CategoryMapper;
import com.goods.povos.CategoryPovo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryPovo> loadCategories() {
        List<CategoryPovo> dadas=categoryMapper.selectAllDad();
        return dadas;
    }

    public void addOneCategory(Category category) {
        categoryMapper.insertOne(category);
    }

    public void addTwoCategory(Category category) {
        categoryMapper.insertTwo(category);
    }

    public void deleteCategory(String cid) {
        categoryMapper.deleteByPrimaryKey(cid);
    }

    public void updateCategory(Category category) {
//        查找已有
        Category categoryById = getCategoryById(category.getCid());
        if(categoryById!=null){
            categoryById.setCname(category.getCname());
            categoryById.setMiaoshu(category.getMiaoshu());
            categoryById.setPid(category.getPid());
            categoryMapper.updateByPrimaryKey(categoryById);
        }
    }

    public Category getCategoryById(String cid) {
        return categoryMapper.selectByPrimaryKey(cid);
    }
}
